home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk4 / patch / patch.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  19KB  |  821 lines

  1. char rcsid[] =
  2.     "$Header: patch.c,v 2.0.1.4 87/02/16 14:00:04 lwall Exp $";
  3.  
  4. /* patch - a program to apply diffs to original files
  5.  *
  6.  * Copyright 1986, Larry Wall
  7.  *
  8.  * This program may be copied as long as you don't try to make any
  9.  * money off of it, or pretend that you wrote it.
  10.  *
  11.  * $Log:    patch.c,v $
  12.  * Revision 2.0.1.4  87/02/16  14:00:04  lwall
  13.  * Short replacement caused spurious "Out of sync" message.
  14.  * 
  15.  * Revision 2.0.1.3  87/01/30  22:45:50  lwall
  16.  * Improved diagnostic on sync error.
  17.  * Moved do_ed_script() to pch.c.
  18.  * 
  19.  * Revision 2.0.1.2  86/11/21  09:39:15  lwall
  20.  * Fuzz factor caused offset of installed lines.
  21.  * 
  22.  * Revision 2.0.1.1  86/10/29  13:10:22  lwall
  23.  * Backwards search could terminate prematurely.
  24.  * 
  25.  * Revision 2.0  86/09/17  15:37:32  lwall
  26.  * Baseline for netwide release.
  27.  * 
  28.  * Revision 1.5  86/08/01  20:53:24  lwall
  29.  * Changed some %d's to %ld's.
  30.  * Linted.
  31.  * 
  32.  * Revision 1.4  86/08/01  19:17:29  lwall
  33.  * Fixes for machines that can't vararg.
  34.  * Added fuzz factor.
  35.  * Generalized -p.
  36.  * General cleanup.
  37.  * 
  38.  * 85/08/15 van%ucbmonet@berkeley
  39.  * Changes for 4.3bsd diff -c.
  40.  *
  41.  * Revision 1.3  85/03/26  15:07:43  lwall
  42.  * Frozen.
  43.  * 
  44.  * Revision 1.2.1.9  85/03/12  17:03:35  lwall
  45.  * Changed pfp->_file to fileno(pfp).
  46.  * 
  47.  * Revision 1.2.1.8  85/03/12  16:30:43  lwall
  48.  * Check i_ptr and i_womp to make sure they aren't null before freeing.
  49.  * Also allow ed output to be suppressed.
  50.  * 
  51.  * Revision 1.2.1.7  85/03/12  15:56:13  lwall
  52.  * Added -p option from jromine@uci-750a.
  53.  * 
  54.  * Revision 1.2.1.6  85/03/12  12:12:51  lwall
  55.  * Now checks for normalness of file to patch.
  56.  * 
  57.  * Revision 1.2.1.5  85/03/12  11:52:12  lwall
  58.  * Added -D (#ifdef) option from joe@fluke.
  59.  * 
  60.  * Revision 1.2.1.4  84/12/06  11:14:15  lwall
  61.  * Made smarter about SCCS subdirectories.
  62.  * 
  63.  * Revision 1.2.1.3  84/12/05  11:18:43  lwall
  64.  * Added -l switch to do loose string comparison.
  65.  * 
  66.  * Revision 1.2.1.2  84/12/04  09:47:13  lwall
  67.  * Failed hunk count not reset on multiple patch file.
  68.  * 
  69.  * Revision 1.2.1.1  84/12/04  09:42:37  lwall
  70.  * Branch for sdcrdcf changes.
  71.  * 
  72.  * Revision 1.2  84/11/29  13:29:51  lwall
  73.  * Linted.  Identifiers uniqified.  Fixed i_ptr malloc() bug.  Fixed
  74.  * multiple calls to mktemp().  Will now work on machines that can only
  75.  * read 32767 chars.  Added -R option for diffs with new and old swapped.
  76.  * Various cosmetic changes.
  77.  * 
  78.  * Revision 1.1  84/11/09  17:03:58  lwall
  79.  * Initial revision
  80.  * 
  81.  */
  82.  
  83. #include "INTERN.h"
  84. #include "common.h"
  85. #include "EXTERN.h"
  86. #include "version.h"
  87. #include "util.h"
  88. #include "pch.h"
  89. #include "inp.h"
  90.  
  91. /* procedures */
  92.  
  93. void reinitialize_almost_everything();
  94. void get_some_switches();
  95. LINENUM locate_hunk();
  96. void abort_hunk();
  97. void apply_hunk();
  98. void init_output();
  99. void init_reject();
  100. void copy_till();
  101. void spew_output();
  102. void dump_line();
  103. bool patch_match();
  104. bool similar();
  105. void re_input();
  106. void my_exit();
  107.  
  108. /* Apply a set of diffs as appropriate. */
  109.  
  110. main(argc,argv)
  111. int argc;
  112. char **argv;
  113. {
  114.     LINENUM where;
  115.     LINENUM newwhere;
  116.     LINENUM fuzz;
  117.     LINENUM mymaxfuzz;
  118.     int hunk = 0;
  119.     int failed = 0;
  120.     int i;
  121.  
  122.     setbuf(stderr, serrbuf);
  123.     for (i = 0; i<MAXFILEC; i++)
  124.     filearg[i] = Nullch;
  125. #ifndef AMIGA
  126.     Mktemp(TMPOUTNAME);
  127.     Mktemp(TMPINNAME);
  128.     Mktemp(TMPREJNAME);
  129.     Mktemp(TMPPATNAME);
  130. #endif
  131.     initMachSpecific();
  132.  
  133.     /* parse switches */
  134.     Argc = argc;
  135.     Argv = argv;
  136.     get_some_switches();
  137.     
  138.     /* make sure we clean up /tmp in case of disaster */
  139.     set_signals();
  140.  
  141.     for (
  142.     open_patch_file(filearg[1]);
  143.     there_is_another_patch();
  144.     reinitialize_almost_everything()
  145.     ) {                    /* for each patch in patch file */
  146.  
  147.     if (outname == Nullch)
  148.         outname = savestr(filearg[0]);
  149.     
  150.     /* initialize the patched file */
  151.     if (!skip_rest_of_patch)
  152.         init_output(TMPOUTNAME);
  153.     
  154.     /* for ed script just up and do it and exit */
  155.     if (diff_type == ED_DIFF) {
  156. #ifdef AMIGA
  157.         say1("Handling of ed diffs not implemented.\n");
  158.         skip_rest_of_patch = TRUE;
  159. #else
  160.         do_ed_script();
  161. #endif
  162.         continue;
  163.     }
  164.     
  165.     /* initialize reject file */
  166.     init_reject(TMPREJNAME);
  167.     
  168.     /* find out where all the lines are */
  169.     if (!skip_rest_of_patch)
  170.         scan_input(filearg[0]);
  171.     
  172.     /* from here on, open no standard i/o files, because malloc */
  173.     /* might misfire and we can't catch it easily */
  174.     
  175.     /* apply each hunk of patch */
  176.     hunk = 0;
  177.     failed = 0;
  178.     out_of_mem = FALSE;
  179.     while (another_hunk()) {
  180.         hunk++;
  181.         fuzz = Nulline;
  182.         mymaxfuzz = pch_context();
  183.         if (maxfuzz < mymaxfuzz)
  184.         mymaxfuzz = maxfuzz;
  185.         if (!skip_rest_of_patch) {
  186.         do {
  187.             where = locate_hunk(fuzz);
  188.             if (hunk == 1 && where == Nulline && !force) {
  189.                         /* dwim for reversed patch? */
  190.             if (!pch_swap()) {
  191.                 if (fuzz == Nulline)
  192.                 say1(
  193. "Not enough memory to try swapped hunk!  Assuming unswapped.\n");
  194.                 continue;
  195.             }
  196.             reverse = !reverse;
  197.             where = locate_hunk(fuzz);  /* try again */
  198.             if (where == Nulline) {        /* didn't find it swapped */
  199.                 if (!pch_swap())         /* put it back to normal */
  200.                 fatal1("Lost hunk on alloc error!\n");
  201.                 reverse = !reverse;
  202.             }
  203.             else if (noreverse) {
  204.                 if (!pch_swap())         /* put it back to normal */
  205.                 fatal1("Lost hunk on alloc error!\n");
  206.                 reverse = !reverse;
  207.                 say1(
  208. "Ignoring previously applied (or reversed) patch.\n");
  209.                 skip_rest_of_patch = TRUE;
  210.             }
  211.             else {
  212. #ifdef AMIGA
  213.                 ask3(
  214. "%seversed (or previously applied) patch detected!  %s -R?",
  215.                 reverse ? "R" : "Unr",
  216.                 reverse ? "Assume" : "Ignore");
  217. #else
  218.                 ask3(
  219. "%seversed (or previously applied) patch detected!  %s -R? [y] ",
  220.                 reverse ? "R" : "Unr",
  221.                 reverse ? "Assume" : "Ignore");
  222. #endif
  223.                 if (*buf == 'n') {
  224. #ifdef AMIGA
  225.                 ask1("Apply anyway?");
  226. #else
  227.                 ask1("Apply anyway? [n] ");
  228. #endif
  229.                 if (*buf != 'y')
  230.                     skip_rest_of_patch = TRUE;
  231.                 where = Nulline;
  232.                 reverse = !reverse;
  233.                 if (!pch_swap())  /* put it back to normal */
  234.                     fatal1("Lost hunk on alloc error!\n");
  235.                 }
  236.             }
  237.             }
  238.         } while (!skip_rest_of_patch && where == Nulline &&
  239.             ++fuzz <= mymaxfuzz);
  240.  
  241.         if (skip_rest_of_patch) {        /* just got decided */
  242.             Fclose(ofp);
  243.             ofp = Nullfp;
  244.         }
  245.         }
  246.  
  247.         newwhere = pch_newfirst() + last_offset;
  248.         if (skip_rest_of_patch) {
  249.         abort_hunk();
  250.         failed++;
  251.         if (verbose)
  252.             say3("Hunk #%d ignored at %ld.\n", hunk, newwhere);
  253.         }
  254.         else if (where == Nulline) {
  255.         abort_hunk();
  256.         failed++;
  257.         if (verbose)
  258.             say3("Hunk #%d failed at %ld.\n", hunk, newwhere);
  259.         }
  260.         else {
  261.         apply_hunk(where);
  262.         if (verbose) {
  263.             say3("Hunk #%d succeeded at %ld", hunk, newwhere);
  264.             if (fuzz)
  265.             say2(" with fuzz %ld", fuzz);
  266.             if (last_offset)
  267.             say3(" (offset %ld line%s)",
  268.                 last_offset, last_offset==1L?"":"s");
  269.             say1(".\n");
  270.         }
  271.         }
  272.     }
  273.  
  274.     if (out_of_mem && using_plan_a) {
  275.         Argc = Argc_last;
  276.         Argv = Argv_last;
  277.         say1("\n\nRan out of memory using Plan A--trying again...\n\n");
  278.         continue;
  279.     }
  280.     
  281.     assert(hunk);
  282.     
  283.     /* finish spewing out the new file */
  284.     if (!skip_rest_of_patch)
  285.         spew_output();
  286.     
  287.     /* and put the output where desired */
  288.     ignore_signals();
  289.     if (!skip_rest_of_patch) {
  290.         if (move_file(TMPOUTNAME, outname) < 0) {
  291.         toutkeep = TRUE;
  292.         chmod(TMPOUTNAME, filemode);
  293.         }
  294.         else
  295.         chmod(outname, filemode);
  296.     }
  297.     Fclose(rejfp);
  298.     rejfp = Nullfp;
  299.     if (failed) {
  300.         if (!*rejname) {
  301.         Strcpy(rejname, outname);
  302.         Strcat(rejname, ".rej");
  303.         }
  304.         if (skip_rest_of_patch) {
  305.         say4("%d out of %d hunks ignored--saving rejects to %s\n",
  306.             failed, hunk, rejname);
  307.         }
  308.         else {
  309.         say4("%d out of %d hunks failed--saving rejects to %s\n",
  310.             failed, hunk, rejname);
  311.         }
  312.         if (move_file(TMPREJNAME, rejname) < 0)
  313.         trejkeep = TRUE;
  314.     }
  315.     set_signals();
  316.     }
  317.     my_exit(0);
  318. }
  319.  
  320. /* Prepare to find the next patch to do in the patch file. */
  321.  
  322. void
  323. reinitialize_almost_everything()
  324. {
  325.     re_patch();
  326.     re_input();
  327.  
  328.     input_lines = 0;
  329.     last_frozen_line = 0;
  330.  
  331.     filec = 0;
  332.     if (filearg[0] != Nullch && !out_of_mem) {
  333.     free(filearg[0]);
  334.     filearg[0] = Nullch;
  335.     }
  336.  
  337.     if (outname != Nullch) {
  338.     free(outname);
  339.     outname = Nullch;
  340.     }
  341.  
  342.     last_offset = 0;
  343.  
  344.     diff_type = 0;
  345.  
  346.     if (revision != Nullch) {
  347.     free(revision);
  348.     revision = Nullch;
  349.     }
  350.  
  351.     reverse = FALSE;
  352.     skip_rest_of_patch = FALSE;
  353.  
  354.     get_some_switches();
  355.  
  356.     if (filec >= 2)
  357.     fatal1("You may not change to a different patch file.\n");
  358. }
  359.  
  360. /* Process switches and filenames up to next '+' or end of list. */
  361.  
  362. void
  363. get_some_switches()
  364. {
  365.     Reg1 char *s;
  366.  
  367.     rejname[0] = '\0';
  368.     Argc_last = Argc;
  369.     Argv_last = Argv;
  370.     if (!Argc)
  371.     return;
  372.     for (Argc--,Argv++; Argc; Argc--,Argv++) {
  373.     s = Argv[0];
  374.     if (strEQ(s, "+")) {
  375.         return;            /* + will be skipped by for loop */
  376.     }
  377.     if (*s != '-' || !s[1]) {
  378.         if (filec == MAXFILEC)
  379.         fatal1("Too many file arguments.\n");
  380.         filearg[filec++] = savestr(s);
  381.     }
  382.     else {
  383.         switch (*++s) {
  384.         case 'b':
  385.         origext = savestr(Argv[1]);
  386.         Argc--,Argv++;
  387.         break;
  388.         case 'c':
  389.         diff_type = CONTEXT_DIFF;
  390.         break;
  391.         case 'd':
  392.         if (!*++s) {
  393.             Argc--,Argv++;
  394.             s = Argv[0];
  395.         }
  396.         if (chdir(s) < 0)
  397.             fatal2("Can't cd to %s.\n", s);
  398.         break;
  399.         case 'D':
  400.             do_defines = TRUE;
  401.         if (!*++s) {
  402.             Argc--,Argv++;
  403.             s = Argv[0];
  404.         }
  405.         Sprintf(if_defined, "#ifdef %s\n", s);
  406.         Sprintf(not_defined, "#ifndef %s\n", s);
  407.         Sprintf(end_defined, "#endif /* %s */\n", s);
  408.         break;
  409.         case 'e':
  410.         diff_type = ED_DIFF;
  411.         break;
  412.         case 'f':
  413.         force = TRUE;
  414.         break;
  415.         case 'F':
  416.         if (*++s == '=')
  417.             s++;
  418.         maxfuzz = atoi(s);
  419.         break;
  420.         case 'l':
  421.         canonicalize = TRUE;
  422.         break;
  423.         case 'n':
  424.         diff_type = NORMAL_DIFF;
  425.         break;
  426.         case 'N':
  427.         noreverse = TRUE;
  428.         break;
  429.         case 'o':
  430.         outname = savestr(Argv[1]);
  431.         Argc--,Argv++;
  432.         break;
  433.         case 'p':
  434.         if (*++s == '=')
  435.             s++;
  436.         strippath = atoi(s);
  437.         break;
  438.         case 'r':
  439.         Strcpy(rejname, Argv[1]);
  440.         Argc--,Argv++;
  441.         break;
  442.         case 'R':
  443.         reverse = TRUE;
  444.         break;
  445.         case 's':
  446.         verbose = FALSE;
  447.         break;
  448.         case 'S':
  449.         skip_rest_of_patch = TRUE;
  450.         break;
  451.         case 'v':
  452.         version();
  453.         break;
  454. #ifdef DEBUGGING
  455.         case 'x':
  456.         debug = atoi(s+1);
  457.         break;
  458. #endif
  459.         default:
  460.         fatal2("Unrecognized switch: %s\n", Argv[0]);
  461.         }
  462.     }
  463.     }
  464. }
  465.  
  466. /* Attempt to find the right place to apply this hunk of patch. */
  467.  
  468. LINENUM
  469. locate_hunk(fuzz)
  470. LINENUM fuzz;
  471. {
  472.     Reg1 LINENUM first_guess = pch_first() + last_offset;
  473.     Reg2 LINENUM offset;
  474.     LINENUM pat_lines = pch_ptrn_lines();
  475.     Reg3 LINENUM max_pos_offset = input_lines - first_guess
  476.                 - pat_lines + 1; 
  477.     Reg4 LINENUM max_neg_offset = first_guess - last_frozen_line - 1
  478.                 + pch_context();
  479.  
  480.     if (!pat_lines)            /* null range matches always */
  481.     return first_guess;
  482.     if (max_neg_offset >= first_guess)    /* do not try lines < 0 */
  483.     max_neg_offset = first_guess - 1;
  484.     if (first_guess <= input_lines && patch_match(first_guess, Nulline, fuzz))
  485.     return first_guess;
  486.     for (offset = 1; ; offset++) {
  487.     Reg5 bool check_after = (offset <= max_pos_offset);
  488.     Reg6 bool check_before = (offset <= max_neg_offset);
  489.  
  490.     if (check_after && patch_match(first_guess, offset, fuzz)) {
  491. #ifdef DEBUGGING
  492.         if (debug & 1)
  493.         say3("Offset changing from %ld to %ld\n", last_offset, offset);
  494. #endif
  495.         last_offset = offset;
  496.         return first_guess+offset;
  497.     }
  498.     else if (check_before && patch_match(first_guess, -offset, fuzz)) {
  499. #ifdef DEBUGGING
  500.         if (debug & 1)
  501.         say3("Offset changing from %ld to %ld\n", last_offset, -offset);
  502. #endif
  503.         last_offset = -offset;
  504.         return first_guess-offset;
  505.     }
  506.     else if (!check_before && !check_after)
  507.         return Nulline;
  508.     }
  509. }
  510.  
  511. /* We did not find the pattern, dump out the hunk so they can handle it. */
  512.  
  513. void
  514. abort_hunk()
  515. {
  516.     Reg1 LINENUM i;
  517.     Reg2 LINENUM pat_end = pch_end();
  518.     /* add in last_offset to guess the same as the previous successful hunk */
  519.     LINENUM oldfirst = pch_first() + last_offset;
  520.     LINENUM newfirst = pch_newfirst() + last_offset;
  521.     LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1;
  522.     LINENUM newlast = newfirst + pch_repl_lines() - 1;
  523.     char *stars = (diff_type == NEW_CONTEXT_DIFF ? " ****" : "");
  524.     char *minuses = (diff_type == NEW_CONTEXT_DIFF ? " ----" : " -----");
  525.  
  526.     fprintf(rejfp, "***************\n");
  527.     for (i=0; i<=pat_end; i++) {
  528.     switch (pch_char(i)) {
  529.     case '*':
  530.         if (oldlast < oldfirst)
  531.         fprintf(rejfp, "*** 0%s\n", stars);
  532.         else if (oldlast == oldfirst)
  533.         fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
  534.         else
  535.         fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst, oldlast, stars);
  536.         break;
  537.     case '=':
  538.         if (newlast < newfirst)
  539.         fprintf(rejfp, "--- 0%s\n", minuses);
  540.         else if (newlast == newfirst)
  541.         fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
  542.         else
  543.         fprintf(rejfp, "--- %ld,%ld%s\n", newfirst, newlast, minuses);
  544.         break;
  545.     case '\n':
  546.         fprintf(rejfp, "%s", pfetch(i));
  547.         break;
  548.     case ' ': case '-': case '+': case '!':
  549.         fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
  550.         break;
  551.     default:
  552.         say1("Fatal internal error in abort_hunk().\n"); 
  553.         abort();
  554.     }
  555.     }
  556. }
  557.  
  558. /* We found where to apply it (we hope), so do it. */
  559.  
  560. void
  561. apply_hunk(where)
  562. LINENUM where;
  563. {
  564.     Reg1 LINENUM old = 1;
  565.     Reg2 LINENUM lastline = pch_ptrn_lines();
  566.     Reg3 LINENUM new = lastline+1;
  567. #define OUTSIDE 0
  568. #define IN_IFNDEF 1
  569. #define IN_IFDEF 2
  570. #define IN_ELSE 3
  571.     Reg4 int def_state = OUTSIDE;
  572.     Reg5 bool R_do_defines = do_defines;
  573.     Reg6 LINENUM pat_end = pch_end();
  574.  
  575.     where--;
  576.     while (pch_char(new) == '=' || pch_char(new) == '\n')
  577.     new++;
  578.     
  579.     while (old <= lastline) {
  580.     if (pch_char(old) == '-') {
  581.         copy_till(where + old - 1);
  582.         if (R_do_defines) {
  583.         if (def_state == OUTSIDE) {
  584.             fputs(not_defined, ofp);
  585.             def_state = IN_IFNDEF;
  586.         }
  587.         else if (def_state == IN_IFDEF) {
  588.             fputs(else_defined, ofp);
  589.             def_state = IN_ELSE;
  590.         }
  591.         fputs(pfetch(old), ofp);
  592.         }
  593.         last_frozen_line++;
  594.         old++;
  595.     }
  596.     else if (new > pat_end)
  597.         break;
  598.     else if (pch_char(new) == '+') {
  599.         copy_till(where + old - 1);
  600.         if (R_do_defines) {
  601.         if (def_state == IN_IFNDEF) {
  602.             fputs(else_defined, ofp);
  603.             def_state = IN_ELSE;
  604.         }
  605.         else if (def_state == OUTSIDE) {
  606.             fputs(if_defined, ofp);
  607.             def_state = IN_IFDEF;
  608.         }
  609.         }
  610.         fputs(pfetch(new), ofp);
  611.         new++;
  612.     }
  613.     else {
  614.         if (pch_char(new) != pch_char(old)) {
  615.         say3(
  616. "Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
  617.             pch_hunk_beg() + old,
  618.             pch_hunk_beg() + new);
  619. #ifdef DEBUGGING
  620.         say3("oldchar = '%c', newchar = '%c'\n",
  621.             pch_char(old), pch_char(new));
  622. #endif
  623.         my_exit(1);
  624.         }
  625.         if (pch_char(new) == '!') {
  626.         copy_till(where + old - 1);
  627.         if (R_do_defines) {
  628.            fputs(not_defined, ofp);
  629.            def_state = IN_IFNDEF;
  630.         }
  631.         while (pch_char(old) == '!') {
  632.             if (R_do_defines) {
  633.             fputs(pfetch(old), ofp);
  634.             }
  635.             last_frozen_line++;
  636.             old++;
  637.         }
  638.         if (R_do_defines) {
  639.             fputs(else_defined, ofp);
  640.             def_state = IN_ELSE;
  641.         }
  642.         while (pch_char(new) == '!') {
  643.             fputs(pfetch(new), ofp);
  644.             new++;
  645.         }
  646.         if (R_do_defines) {
  647.             fputs(end_defined, ofp);
  648.             def_state = OUTSIDE;
  649.         }
  650.         }
  651.         else {
  652.         assert(pch_char(new) == ' ');
  653.         old++;
  654.         new++;
  655.         }
  656.     }
  657.     }
  658.     if (new <= pat_end && pch_char(new) == '+') {
  659.     copy_till(where + old - 1);
  660.     if (R_do_defines) {
  661.         if (def_state == OUTSIDE) {
  662.             fputs(if_defined, ofp);
  663.         def_state = IN_IFDEF;
  664.         }
  665.         else if (def_state == IN_IFNDEF) {
  666.         fputs(else_defined, ofp);
  667.         def_state = IN_ELSE;
  668.         }
  669.     }
  670.     while (new <= pat_end && pch_char(new) == '+') {
  671.         fputs(pfetch(new), ofp);
  672.         new++;
  673.     }
  674.     }
  675.     if (R_do_defines && def_state != OUTSIDE) {
  676.     fputs(end_defined, ofp);
  677.     }
  678. }
  679.  
  680. /* Open the new file. */
  681.  
  682. void
  683. init_output(name)
  684. char *name;
  685. {
  686.     ofp = fopen(name, "w");
  687.     if (ofp == Nullfp)
  688.     fatal2("patch: can't create %s.\n", name);
  689. }
  690.  
  691. /* Open a file to put hunks we can't locate. */
  692.  
  693. void
  694. init_reject(name)
  695. char *name;
  696. {
  697.     rejfp = fopen(name, "w");
  698.     if (rejfp == Nullfp)
  699.     fatal2("patch: can't create %s.\n", name);
  700. }
  701.  
  702. /* Copy input file to output, up to wherever hunk is to be applied. */
  703.  
  704. void
  705. copy_till(lastline)
  706. Reg1 LINENUM lastline;
  707. {
  708.     Reg2 LINENUM R_last_frozen_line = last_frozen_line;
  709.  
  710.     if (R_last_frozen_line > lastline)
  711.     say1("patch: misordered hunks! output will be garbled.\n");
  712.     while (R_last_frozen_line < lastline) {
  713.     dump_line(++R_last_frozen_line);
  714.     }
  715.     last_frozen_line = R_last_frozen_line;
  716. }
  717.  
  718. /* Finish copying the input file to the output file. */
  719.  
  720. void
  721. spew_output()
  722. {
  723. #ifdef DEBUGGING
  724.     if (debug & 256)
  725.     say3("il=%ld lfl=%ld\n",input_lines,last_frozen_line);
  726. #endif
  727.     if (input_lines)
  728.     copy_till(input_lines);        /* dump remainder of file */
  729.     Fclose(ofp);
  730.     ofp = Nullfp;
  731. }
  732.  
  733. /* Copy one line from input to output. */
  734.  
  735. void
  736. dump_line(line)
  737. LINENUM line;
  738. {
  739.     Reg1 char *s;
  740.     Reg2 char R_newline = '\n';
  741.  
  742.     /* Note: string is not null terminated. */
  743.     for (s=ifetch(line, 0); putc(*s, ofp) != R_newline; s++) ;
  744. }
  745.  
  746. /* Does the patch pattern match at line base+offset? */
  747.  
  748. bool
  749. patch_match(base, offset, fuzz)
  750. LINENUM base;
  751. LINENUM offset;
  752. LINENUM fuzz;
  753. {
  754.     Reg1 LINENUM pline = 1 + fuzz;
  755.     Reg2 LINENUM iline;
  756.     Reg3 LINENUM pat_lines = pch_ptrn_lines() - fuzz;
  757.  
  758.     for (iline=base+offset+fuzz; pline <= pat_lines; pline++,iline++) {
  759.     if (canonicalize) {
  760.         if (!similar(ifetch(iline, (offset >= 0)),
  761.              pfetch(pline),
  762.              pch_line_len(pline) ))
  763.         return FALSE;
  764.     }
  765.     else if (strnNE(ifetch(iline, (offset >= 0)),
  766.            pfetch(pline),
  767.            pch_line_len(pline) ))
  768.         return FALSE;
  769.     }
  770.     return TRUE;
  771. }
  772.  
  773. /* Do two lines match with canonicalized white space? */
  774.  
  775. bool
  776. similar(a,b,len)
  777. Reg1 char *a;
  778. Reg2 char *b;
  779. Reg3 int len;
  780. {
  781.     while (len) {
  782.     if (isspace(*b)) {        /* whitespace (or \n) to match? */
  783.         if (!isspace(*a))        /* no corresponding whitespace? */
  784.         return FALSE;
  785.         while (len && isspace(*b) && *b != '\n')
  786.         b++,len--;        /* skip pattern whitespace */
  787.         while (isspace(*a) && *a != '\n')
  788.         a++;            /* skip target whitespace */
  789.         if (*a == '\n' || *b == '\n')
  790.         return (*a == *b);    /* should end in sync */
  791.     }
  792.     else if (*a++ != *b++)        /* match non-whitespace chars */
  793.         return FALSE;
  794.     else
  795.         len--;            /* probably not necessary */
  796.     }
  797.     return TRUE;            /* actually, this is not reached */
  798.                     /* since there is always a \n */
  799. }
  800.  
  801. /* Exit with cleanup. */
  802.  
  803. void
  804. my_exit(status)
  805. int status;
  806. {
  807.     Unlink(TMPINNAME);
  808.     if (!toutkeep) {
  809.     Unlink(TMPOUTNAME);
  810.     }
  811.     if (!trejkeep) {
  812.     Unlink(TMPREJNAME);
  813.     }
  814.     if(pfp) {
  815.     fclose(pfp);
  816.     }
  817.     Unlink(TMPPATNAME);
  818.     cleanMachSpecific();
  819.     exit(status);
  820. }
  821.